#!/usr/bin/env python
"""
Nagios plugin that queries NorduGrid ARC version.

WARN: requires ngtest (arc client tools) to be installed on front-end
"""
# last change   -> shorter 'output' message due to nodutils 255 char limits

__author__ = "Placi Flury grid@switch.ch"
__copyright__ = "Copyright 2010, SMSCG an AAA/SWITCH project"
__date__ = "25.08.2009"
__version__ = "0.1.1"

import sys,commands
import os.path
from nagios_plugin import NagiosPluginBasic
from nagios_plugin import RETURN_CODE

NG_INSTALL_PATH= "/usr/"

class NGVersion(NagiosPluginBasic):
    """
    Querie NorduGrid ARC version.
    """
    def __init__(self):
        parser = NagiosPluginBasic.getOptions(self)
        parser.remove_option("-c")   # removing thresholds
        parser.remove_option("-w")
        parser.remove_option("-H")   # remove hostname option
        parser.parse_args()


    def main(self):
        ngtest = os.path.join(NG_INSTALL_PATH,"bin","arctest")
        if not os.path.isfile(ngtest):    
            print "CRITICAL: could not find executable '%s'" % ngtest
            sys.exit(RETURN_CODE["critical"])
        try:
            cmd = "%s -v " % ngtest
            str=commands.getoutput(cmd)
            version = str.split(" ")[2]
        except:
            print "UNKNOWN: couldn't get ARCversion."
            sys.exit(RETURN_CODE["unknown"])
        
        print "OK: Version %s " % version
        sys.exit(RETURN_CODE["ok"])
            
if __name__ == "__main__":
    g = NGVersion()
    g.main()
